package cn.ezios.huo.system.biz.controller;

import cn.ezios.huo.system.dao.domain.SysUserRole;
import cn.ezios.huo.system.dao.dto.SysUserRoleDTO;
import cn.ezios.huo.system.dao.vo.SysUserRoleVO;
import cn.ezios.huo.system.dao.service.SysUserRoleService;
import cn.ezios.huo.common.mvc.Ret;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("sys/user/role")
@RequiredArgsConstructor
public class SysUserRoleController {

    private final SysUserRoleService sysUserRoleService;

    @PostMapping("")
    public Ret add(@RequestBody SysUserRole sysUserRole) {
        boolean save = sysUserRoleService.save(sysUserRole);
        return save ? Ret.ok() : Ret.error();
    }

    @PutMapping("")
    public Ret update(@RequestBody SysUserRole sysUserRole) {
        boolean update = sysUserRoleService.updateById(sysUserRole);
        return update ? Ret.ok() : Ret.error();
    }

    @GetMapping("{id}")
    public Ret get(@PathVariable("id") String id) {
        SysUserRole sysUserRole = sysUserRoleService.getById(id);
        return sysUserRole != null ? Ret.ok(sysUserRole) : Ret.error("未找到");
    }

//    @GetMapping("page")
//    public Ret page(@RequestParam(value = "current", defaultValue = "1") Integer current,
//    @RequestParam(value = "size", defaultValue = "10") Integer size,SysUserRoleDTO dto) {
//       IPage <SysUserRoleVO> page = sysUserRoleService.queryPage(current,size,dto);
//            return Ret.ok(page);
//    }

    @DeleteMapping("{id}")
    public Ret delete(@PathVariable("id") String id) {
        boolean remove = sysUserRoleService.removeById(id);
        return remove ? Ret.ok() : Ret.error();
    }

    @GetMapping("get-user-role-ids/{userId}")
    public Ret getUserRoleIds(@PathVariable("userId") String userId) {
        return Ret.ok(sysUserRoleService.getUserRoleIds(userId));
    }

    @PutMapping("update-user-roles")
    public Ret updateUserRoles(@RequestBody SysUserRoleDTO sysUserRoleDTO){
        if(sysUserRoleDTO.getUserId()==null||sysUserRoleDTO.getUserId().isBlank()){
            return Ret.error("用户ID不能为空");
        }
        boolean b = sysUserRoleService.updateUserRole(sysUserRoleDTO.getUserId(), sysUserRoleDTO.getRoleIds());
        return b ? Ret.ok() : Ret.error();
    }
}
